/*
 *    Copyright (c) 2018-2025, nart All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: nart
 */

package com.nai365.nart.product.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nai365.nart.common.core.constant.SecurityConstants;
import com.nai365.nart.common.core.util.R;
import com.nai365.nart.common.excel.annotation.ResponseExcel;
import com.nai365.nart.common.log.annotation.SysLog;
import com.nai365.nart.common.security.annotation.Inner;
import com.nai365.nart.product.dto.SkuUpTo;
import com.nai365.nart.product.entity.SkuInfo;
import com.nai365.nart.product.service.SkuInfoService;
import com.nai365.nart.search.api.feign.SearchFeignService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * sku信息
 *
 * @author baogs
 * @date 2022-07-01 11:05:45
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/skuinfo")
@Api(value = "skuinfo", tags = "sku信息管理")
public class SkuInfoController {

	private final SkuInfoService skuInfoService;
	private final SearchFeignService searchFeignService;

	/**
	 * 分页查询
	 *
	 * @param page    分页对象
	 * @param skuInfo sku信息
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	public R getSkuInfoPage(Page page, SkuInfo skuInfo) {
		return R.ok(skuInfoService.page(page, Wrappers.query(skuInfo)));
	}


	//商品上架
	///product/spuinfo/{spuId}/up
	@ApiModelProperty(value = "根据skuId上架", notes = "根据skuId上架")
	@PostMapping(value = "/up_sku")
	public R skuUp(@RequestBody SkuUpTo skuUpTo) {
		return skuInfoService.up(skuUpTo.getSkuIds(), skuUpTo.getPrice());
	}

	//商品下架
	///product/spuinfo/down
	@ApiModelProperty(value = "根据skuId下架", notes = "根据skuId下架")
	@GetMapping(value = "/down")
	public R skuDown(Long skuId) {
		boolean down = skuInfoService.down(skuId);
		if (!down) {
			return R.failed("下架失败");
		} else {
			return R.ok(searchFeignService.productSkuDown(skuId, SecurityConstants.FROM_IN));
		}
	}

	/**
	 * 通过id查询sku信息
	 *
	 * @param skuId id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{skuId}")
	public R getById(@PathVariable("skuId") Long skuId) {
		return R.ok(skuInfoService.getById(skuId));
	}


	/**
	 * 新增sku信息
	 *
	 * @param skuInfo sku信息
	 * @return R
	 */
	@ApiOperation(value = "新增sku信息", notes = "新增sku信息")
	@SysLog("新增sku信息")
	@PostMapping
	public R save(@RequestBody SkuInfo skuInfo) {
		return R.ok(skuInfoService.save(skuInfo));
	}

	/**
	 * 修改sku信息
	 *
	 * @param skuInfo sku信息
	 * @return R
	 */
	@ApiOperation(value = "修改sku信息", notes = "修改sku信息")
	@SysLog("修改sku信息")
	@PutMapping
	public R updateById(@RequestBody SkuInfo skuInfo) {
		return R.ok(skuInfoService.updateById(skuInfo));
	}


	/**
	 * 修改sku信息,内部调用
	 *
	 * @param skuInfo sku信息
	 * @return R
	 */
	@ApiOperation(value = "修改sku信息", notes = "修改sku信息")
	@SysLog("修改sku信息")
	@PutMapping("/inner")
	@Inner
	public R<SkuInfo> updateByIdInner(@RequestBody SkuInfo skuInfo) {
		skuInfoService.updateById(skuInfo);
		return R.ok(skuInfo);
	}

	/**
	 * 通过id删除sku信息
	 *
	 * @param skuId id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除sku信息", notes = "通过id删除sku信息")
	@SysLog("通过id删除sku信息")
	@DeleteMapping("/{skuId}")
	public R removeById(@PathVariable Long skuId) {
		return R.ok(skuInfoService.removeById(skuId));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param skuInfo 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	public List<SkuInfo> export(SkuInfo skuInfo) {
		return skuInfoService.list(Wrappers.query(skuInfo));
	}


	/**
	 * 信息
	 */
	@GetMapping("/info/{skuId}")
	@Inner
	public R info(@PathVariable("skuId") Long skuId) {
		SkuInfo skuInfo = skuInfoService.getById(skuId);

		return R.ok(skuInfo);
	}

	/**
	 * 查询商品集合
	 */
	@PostMapping("/infos")
	@Inner
	public R<List<SkuInfo>> infos(@RequestBody List<Long> skuIds) {
		List<SkuInfo> skuInfos = skuInfoService.getBaseMapper().selectBatchIds(skuIds);
		return R.ok(skuInfos);
	}


	/**
	 * 查詢可用sku
	 */
	@GetMapping("my/{spuId}/{status}")
	public R<List<SkuInfo>> findMySkuBySpuIdAndStatus(@PathVariable("spuId") Long spuId, @PathVariable("status") Integer status) {
		return R.ok(skuInfoService.findMySkuBySpuIdAndStatus(spuId, status));
	}

	/**
	 * 锁定一个可用的sku
	 *
	 * @param spuId spu id
	 * @return SkuInfo
	 */
	@PutMapping("/lock/{spuId}")
	public R<SkuInfo> lockSkuInfo(@PathVariable("spuId") Long spuId) {
		try {
			return R.ok(this.skuInfoService.lockSkuInfo(spuId), "藏品锁定成功");
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@PutMapping("/unlock")
	public R<SkuInfo> unlockSkuInfo(@RequestBody SkuInfo skuInfo) {
		return R.ok(this.skuInfoService.unLockSkuInfo(skuInfo), "藏品解锁成功");
	}

	/**
	 * 批量更新Sku
	 *
	 * @param skuInfoList sku列表
	 * @return sku列表
	 */
	@PutMapping("/updateBatch")
	public R<List<SkuInfo>> updateBatch(@RequestBody List<SkuInfo> skuInfoList) {
		skuInfoService.updateBatchById(skuInfoList);
		return R.ok(skuInfoList, "批量保存成功");
	}

	@PutMapping("/findSkuInfoBySkuId/{skuId}")
	@Inner
	public SkuInfo findSkuInfoBySkuId(@PathVariable("skuId") Long skuId) {
		return skuInfoService.findSkuInfoBySkuId(skuId);
	}

}
